using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.ComponentModel;

namespace MediaData
{
    public class MediaUtil
    {
        public static string[] BasicVideoExtensions = new string[] { "mkv", "mp4", "avi", "mpg", "ogm", "wmv", "mov", "divx", "ifo" };
        public static string[] BasicDiscImageExtensions = new string[] { "img", "cue", "iso" };
        public static string[] BasicPictureExtensions = new string[] { "jpg", "png", "gif" };

        public enum FileType 
        {
            Unknown,
            Video,
            DiscImage,
            Picture
        }
        public static FileType GetFiletype(string filePath)
        {
            string ext = Path.GetExtension(filePath).ToLower();
            if (string.IsNullOrEmpty(ext))
                return FileType.Unknown;

            ext = ext.Substring(1);

            if (Array.IndexOf<string>(BasicVideoExtensions, ext) > -1)
                return FileType.Video;

            if (Array.IndexOf<string>(BasicDiscImageExtensions, ext) > -1)
                return FileType.DiscImage;

            if (Array.IndexOf<string>(BasicPictureExtensions, ext) > -1)
                return FileType.Picture;

            return FileType.Unknown;

        }

        public static string FindImage(string dir, ICollection<string> acceptedExtensions)
        {
            foreach (string f in Directory.GetFiles(dir, "*", SearchOption.AllDirectories))
            {
                string ext = Path.GetExtension(f).ToLower();
                if(ext.Length < 1)
                    continue;
                foreach (string e in acceptedExtensions)
                    if (ext.Substring(1).Equals(e.ToLower()))
                        return f;
            }
            return null;
        }
        public static string[] RelatedFiles(string path)
        {
            string filename = Path.GetFileNameWithoutExtension(path);
            return Directory.GetFiles(Path.GetDirectoryName(path), filename + ".*");
        }
        public static string GetMimeType(string extension)
        {
            switch (extension)
            {
                case "mkv":
                    return "video/x-matroska";
                case "mp4":
                    return "video/mp4v-es";
                case "avi":
                    return "video/x-msvideo";
                case "ogm":
                    return "video/x-mpeg";
                case "mpeg":
                    return "video/mpeg";
                default:
                    return "application/octet-stream";
            }
        }

    }
}
